﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnitLibrary.Managers;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using UnitLibrary.GameObjects.UnitReaders;
using FarseerGames.FarseerPhysics.Collisions;
using UnitLibrary.GameObjects.PowerUpEffects;
using FarseerGames.FarseerPhysics.Mathematics;
using UnitLibrary.Utility;

namespace UnitLibrary.GameObjects.Units
{
    /// <summary>
    /// Player class
    /// </summary>
    public class Player: UnitGen
    {
        /// <summary>
        /// 
        /// </summary>
        public Player()
        {
            this._body.MomentOfInertia = float.MaxValue;
        }

        private int _points = 60;
        private int _pointValue = 1;
        [ContentSerializerIgnore]
        public string CurrentSentry = "Default";
        [ContentSerializerIgnore]
        public int CurrentSentryCost = 20;
        [ContentSerializerIgnore]
        public string CurrentWeapon = "Default";
        
        public int Points
        {
            get { return _points; }
        }

        public void PointAdjust(int pointAmount)
        {
            _points += pointAmount;
        }

        private bool OnCollision(Geom geom1, Geom geom2, ContactList contactList)
        {
            Iid g1tag = (Iid)geom1.Tag;
            Iid g2tag = (Iid)geom2.Tag;
            //Geom 2 tag was never created, we don't know the type, so just return true

            if (geom1.Tag == null)
            {
                Console.WriteLine("Geom 1 tag inside of Enemy.cs OnCollision was never instantiated");
                //Return true since we don't know what the geoms are
                return true;
            }
            if (geom2.Tag == null)
            {
                Console.WriteLine("Geom 2 tag inside of Enemy.cs OnCollision was never instantiated");
                //Return true since we don't know what the geoms are
                return true;
            }
            //Make sure it's an enemy
            if (g1tag.UnitID == UnitID.Player)
            {
                //Go through the different types of unit ids
                switch (g2tag.UnitID)
                {
                    case UnitID.Enemy:
                        Enemy enemy = (Enemy)_components.GetUnit(g2tag);
                        this.Health -= enemy.Damage;                        
                        if (this.Health < 0)
                            if (_components.playerDied != null)
                                _components.playerDied();
                            else
                                Console.WriteLine("The main game screen will not know that the player died!");
                        return true;
                    case UnitID.Points:
                        this._points += _pointValue;
                        _components.RemoveUnit(g2tag, false);
                        return false;
                    case UnitID.PowerUp:                        
                        return false;
                    default:
                        return false;
                }
            }
            return true;
        }
        /// <summary>
        /// Function to copy values from the genericunit to this unit. For use with loading from content pipeline, or for a deep copy from a unit definition
        /// </summary>
        /// <param name="genericUnit">Unit to copy values from</param>
        /// <param name="location">Location to place the player</param>
        /// <param name="direction">Direction which player is facing</param>
        public override void Create(object genericUnit, ComponentM components, Vector2 location, Vector2 direction, bool addtophysics)
        {
            //Have base copy values
            base.ID = new Iid(0, UnitID.Player); 
            base.Create(genericUnit, components, location, direction, addtophysics);
            if (base._geom.OnCollision == null)
                base._geom.OnCollision += this.OnCollision;
            CopyValues((Player)genericUnit);
        }

        private void CopyValues(Player player)
        {

        }

        /// <summary>
        /// Update the player
        /// </summary>
        /// <param name="gametime"></param>
        public override void Update(GameTime gametime)
        {
            this._body.AngularVelocity = 0;
            base.Update(gametime);
        }

        public void HandleInput(InputHelper input)
        {
            //todo move input handling from playerm to here
        }

        private void MovePlayer()
        {
            //todo move movement handling from playerm to here
        }
    }
}
